package com.weiyuwqj.api.util;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;

import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Copyright 2015-2019 Yunfucloud technology co., Ltd.
 * Created by fuxx on 15/11/10.
 */
public class ParamUtils {

//    public static  Map<String,Object> extract(HttpServletRequest request,String[] paramNames){
//        Map<String, Object> map = new HashMap<>();
//        String paramValue=null;
//        for(int i=0;i<paramNames.length;i++){
//            paramValue=request.getParameter(paramNames[i]);
//            if (StringUtils.isNotEmpty(paramValue)) {
//                map.put(paramNames[i], paramValue);
//            }
//        }
//
//        return  map;
//    }

    public static Map<String, Object> getBeanMap(Object bean) {
        Map<String, Object> map = new HashMap<>();
        if (bean == null) return map;
        Object value = null;
        PropertyDescriptor[] props = BeanUtils.getPropertyDescriptors(bean.getClass());
        for (PropertyDescriptor p : props) {
            if ("class".equals(p.getName())) {
                continue;
            }
            try {
                value = p.getReadMethod().invoke(bean, null);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (value == null) {
                continue;
            }
            if (StringUtils.isBlank(value.toString())) {
                continue;
            }
            map.put(p.getName(), value);
        }
        return map;
    }

    public static Map<String, String> getBeanMapForString(Object bean) {
        Map<String, String> map = new HashMap<>();
        if (bean == null) return map;
        String value = null;
        PropertyDescriptor[] props = BeanUtils.getPropertyDescriptors(bean.getClass());
        for (PropertyDescriptor p : props) {
            if ("class".equals(p.getName())) {
                continue;
            }
            try {
                value = (p.getReadMethod().invoke(bean, null)).toString();
            } catch (NullPointerException e) {
                value = "";
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }

            if (value == null) {
                continue;
            }
            if (StringUtils.isBlank(value.toString())) {
                continue;
            }
            map.put(p.getName(), value);
        }
        return map;
    }

    public static Map<String, Object> getBeanMapByList(Object bean, List<Class> classList) {
        Map<String, Object> map = new HashMap<>();
        if (bean == null) return map;
        Object value = null;
        PropertyDescriptor[] props = BeanUtils.getPropertyDescriptors(bean.getClass());
        for (PropertyDescriptor p : props) {
            if ("class".equals(p.getName())) {
                continue;
            }
            try {
                value = p.getReadMethod().invoke(bean, null);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (value == null) {
                continue;
            }
            if (StringUtils.isBlank(value.toString())) {
                continue;
            }
            map.put(p.getName(), value);
        }
        return map;
    }

    public static void main(String[] args) {
/*        BasItem item=new BasItem();
        item.setAddip("8.8.8.8");
        ParamUtils.getBeanMap(item);*/
    }

}
